<template>
  <el-dialog
    v-model="state.dialogVisible"
    :append-to-body="true"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    :destroy-on-close="true"
    top="5vh"
    :modal="model"
    :title="title"
    :width="width"
  >
    <vab-query-form>
      <vab-query-form-top-panel v-show="state.showSearch">
        <el-form
          ref="queryRef"
          :model="tableOptions.queryForm"
          :inline="true"
          label-width="80px"
          @keyup.enter="queryTableData"
        >
          <el-form-item label="流程名称" prop="flowName">
            <el-input
              v-model="tableOptions.queryForm.flowName"
              placeholder="流程名称"
              style="max-width: 180px"
            />
          </el-form-item>
          <el-form-item label="流程key" prop="flowKey">
            <el-input
              v-model="tableOptions.queryForm.flowKey"
              placeholder="流程key"
              style="max-width: 180px"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="refresh-left"
              type="warning"
              @click="handleResetQuery"
              >重置</el-button
            >
            <el-button icon="search" type="primary" @click="queryTableData"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
    </vab-query-form>
    <el-table
      ref="tableRef"
      v-loading="tableOptions.loading"
      border
      :data="tableOptions.dataList"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      :height="tableOptions.height"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
      @sort-change="handleSortChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column type="index" label="序号" width="55" />
      <el-table-column
        prop="id"
        label="流程ID"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        prop="flowName"
        label="流程名称"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        prop="flowKey"
        label="流程Key"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        prop="flowIcon"
        label="图标 "
        show-overflow-tooltip
        width="80"
      >
        <template #default="scope">
          <IconifyIconOnline
            :icon="scope.row.icon.name"
            :style="{
              background: scope.row.icon.bgc,
              color: scope.row.icon.color
            }"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="groupDesc"
        label="流程分组"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        prop="statusDesc"
        label="状态"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        prop="version"
        label="版本"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        prop="remark"
        label="备注"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column
        prop="createUserName"
        label="创建人"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
        show-overflow-tooltip
        width="160"
      />
    </el-table>
    <el-pagination
      background
      :current-page="tableOptions.pagination.current"
      :layout="tableOptions.pagination.layout"
      :page-size="tableOptions.pagination.size"
      :total="tableOptions.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <template #footer>
      <el-button icon="circle-close" type="warning" @click="close">
        取消
      </el-button>
      <el-button icon="circle-check" type="primary" @click="save">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts" name="DefFlowPicker">
import { useTable } from "@/hooks/table";
import { getLatestPage } from "@/api/flow/def";
import { getTableHeight } from "@/utils/util";
import { cloneObject } from "@/utils/common";
import RightToolBar from "@/components/rightToolBar.vue";
import type { BasicTableProps } from "@/hooks/table";
import { reactive, ref } from "vue";

const props = defineProps({
  title: {
    type: String,
    default: "选择流程"
  },
  width: {
    type: String,
    default: "60%"
  },
  model: {
    type: Boolean,
    default: true
  },
  multiple: {
    type: Boolean,
    default: function () {
      return false;
    }
  }
});

const emits = defineEmits(["change"]);

// 搜索变量
const queryRef = ref();
const tableRef = ref(null);

const state = reactive({
  dialogVisible: false,
  showSearch: true
});

const tableOptions = reactive<BasicTableProps>({
  queryForm: {
    flowName: "",
    flowKey: ""
  },
  autoLoad: true,
  queryFunction: getLatestPage,
  descs: ["create_time"]
});

const {
  instance: defFlowTable,
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleReset,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange,
  handleToggleHeight
} = useTable(tableOptions, tableRef);

const handleToggleSearch = () => {
  if (state.showSearch) {
    tableOptions.height = getTableHeight(1, true, true) - 10;
  } else {
    tableOptions.height = getTableHeight(0, true, true);
  }
};

const show = params => {
  cloneObject(defFlowTable.queryForm, params);
  queryTableData();
  handleToggleHeight(20);
  state.dialogVisible = true;
};

const close = () => {
  handleReset();
  state.dialogVisible = false;
};

const save = () => {
  if (!props.multiple) {
    emits("change", defFlowTable.dataSelections[0]);
  } else {
    emits("change", defFlowTable.dataSelections);
  }
  close();
};

defineExpose({
  save,
  show,
  close
});
</script>
<style lang="scss" scoped>
.el-container {
  flex-direction: column;
}
</style>
